package com.dayatang.hrm.compensation.domain;

import java.util.Date;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

import com.dayatang.commons.domain.InstanceFactory;
import com.dayatang.hrm.compensation.policy.DirectlyCalculatePolicy;
import com.dayatang.hrm.compensation.vo.FiscalPeriodRangeSetting;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("3")
public class FixedEmployeeSalaryParameterSetting extends SalaryParameterSetting {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7712557369073682484L;

	private static FixedEmployeeSalaryParameterSettingRepository fixedEmployeeSalaryParameterSettingRepository;

	public FixedEmployeeSalaryParameterSetting() {
		// 只能为：直接取值
		//setDataCalculatePolicy(DirectlyCalculatePolicy.getInstance());
		// 范围设置必须为：PRESENT_FISCAL_PERIOD
		//setFiscalPeriodRangeSetting(FiscalPeriodRangeSetting.PRESENT_FISCAL_PERIOD_SETTING);
	}

	public FixedEmployeeSalaryParameterSetting(
			FixedEmployeeSalaryParameter parameter) {
		this();
		setSalaryParameter(parameter);
	}

	public static FixedEmployeeSalaryParameterSettingRepository getFixedEmployeeSalaryParameterSettingRepository() {
		if (fixedEmployeeSalaryParameterSettingRepository == null) {
			fixedEmployeeSalaryParameterSettingRepository = InstanceFactory
					.getInstance(FixedEmployeeSalaryParameterSettingRepository.class);
		}
		return fixedEmployeeSalaryParameterSettingRepository;
	}

	public static void setFixedEmployeeSalaryParameterSettingRepository(
			FixedEmployeeSalaryParameterSettingRepository fixedEmployeeSalaryParameterSettingRepository) {
		FixedEmployeeSalaryParameterSetting.fixedEmployeeSalaryParameterSettingRepository = fixedEmployeeSalaryParameterSettingRepository;
	}

	/*
	 * =======================================
	 * 
	 * action
	 * 
	 * ========================================
	 */

	public boolean isNameAlreadyUsedByOtherInSameSalaryAccountSuite(
			String name, Date fromDate, Date toDate) {
		if (isNew()) {
			return getFixedEmployeeSalaryParameterSettingRepository()
					.isNameAlreadyUsedExceptSelf(name, fromDate, toDate, null);
		} else {
			return getFixedEmployeeSalaryParameterSettingRepository()
					.isNameAlreadyUsedExceptSelf(name, fromDate, toDate, this);
		}
	}

	public static FixedEmployeeSalaryParameterSetting init(
			FixedEmployeeSalaryParameter parameter) {
		return new FixedEmployeeSalaryParameterSetting(parameter);
	}

	public static FixedEmployeeSalaryParameterSetting get(
			FixedEmployeeSalaryParameter parameter, Date queryDate) {
		return getFixedEmployeeSalaryParameterSettingRepository().get(
				parameter, queryDate);
	}

	@Override
	public SalaryParameterSetting getNewInstance(SalaryParameter salaryParameter) {
		return new FixedEmployeeSalaryParameterSetting(
				(FixedEmployeeSalaryParameter) salaryParameter);
	}

}
